package com.aaa.controller;


import com.aaa.dao.EmpDao;
import com.aaa.entity.Emp;
import com.aaa.service.DeptDaoService;
import com.aaa.service.EmpDaoService;
import com.aaa.vo.EmpSearchForm;
import com.aaa.vo.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author C-Q
 * @version 1.1.0
 * @Description: TODO
 * @date 2022/9/24
 * @Repository和@Controller、@Service、@Component的作用差不多，都是把对象交给spring管理。
 */
@CrossOrigin
@Controller
@RequestMapping("emp")
public class EmpController {

    @Autowired
    private EmpDaoService empDaoService ;

    /**
     * 在页面添加数据，确认添加的数据并回显到前端页面
     * @param emp
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public Result add(@RequestBody Emp emp){
        System.out.println("111111"+emp);
        empDaoService.insert(emp);
        Result result = new Result(200,"确认添加成功");
        return result;
    }

    /**
     * 根据id删除对应行的数据
     * @param request
     * @return
     */

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(HttpServletRequest request){

        String id = request.getParameter("id");
        empDaoService.deleteByPrimaryKey(Integer.parseInt(id));
        Result result = new Result(200,"确认删除成功");
        return result;
    }


    /**
     * 在页面修改数据，确认修改并将修改后的数据回显到前端页面
     * @param emp
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public Result update(@RequestBody Emp emp){
        empDaoService.updateByPrimaryKey(emp);
        Result result = new Result(200,"确认修改成功");
        return result;
    }

    /**
     * 把数据库的数据回传到前端页面
     */
    @RequestMapping("list")
    @ResponseBody
    public Result list(@RequestParam(defaultValue = "1") int currentPage, @RequestParam(defaultValue = "5") int pageSize
    ,@RequestBody EmpSearchForm empSearchForm){
        PageInfo<Emp> all = empDaoService.findAll(currentPage,pageSize,empSearchForm);
        System.out.println(empSearchForm);
//        System.out.println("ABC"+all);
        //把查询结果封装到PageInfo对象
        return new Result(200,"查询成功",all);
//        return null;
    }





}
